package com.pfzy.ui.view.strokeText;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.style.ReplacementSpan;

import androidx.annotation.NonNull;

public class CustomOutlineSpan extends ReplacementSpan {

    private int mOutlineColor;
    private float mOutlineWidth;

    public CustomOutlineSpan(int outlineColor, float outlineWidth) {
        mOutlineColor = outlineColor;
        mOutlineWidth = outlineWidth;
    }

    @Override
    public int getSize(@NonNull Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
        return (int) paint.measureText(text, start, end);
    }

    @Override
    public void draw(@NonNull Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, @NonNull Paint paint) {
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(mOutlineWidth);
        paint.setColor(mOutlineColor);
        canvas.drawText(text, start, end, x, y, paint);

        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.WHITE);
        canvas.drawText(text, start, end, x, y, paint);
    }
}